package com.nicolasprof.profgallery.thumbs {
	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.assets.Arrow;
	import com.nicolasprof.profgallery.thumbs.ThumbControl;

	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author Nicolas Prof
	 */
	public class ArrowControl extends ThumbControl {

		private var _maxScroll : int;
		private var _type : String;
		
		private const INCR : String = "incr";
		private const DECR : String = "decr";

		public function ArrowControl(ref : Thumbs) {
			super(ref);
		}

		override public function build() : void {
			_maxScroll = _thumbs.calcMaxScroll();
			
			if (_maxScroll <= 0) {
				return;
			}
			var config : XML = Engine.CONFIG_XML;
			var margin:int = int(config.arrow.@margin);
			var arrow : Arrow;
			var columns : int = int(config.thumbnail.@cols);
			var rows : int = int(config.thumbnail.@rows);
			var spacingX : int =  int(config.thumbnail.@spaceX);
			var spacingY : int = int(config.thumbnail.@spaceY);
			var thumbWidth : int = int(config.thumbnail.@width);
			var thumbHeight : int = int(config.thumbnail.@height);
			
			
			_type = config.thumbnail.@type;		
			
			switch (_type){
				case Thumbs.VERTICAL :
					arrow = createArrow(DECR);
					arrow.x = columns * (spacingX + thumbWidth)* 0.5;
					arrow.y = -margin;
					
					arrow = createArrow(INCR);
					arrow.x = columns * (spacingX + thumbWidth)* 0.5;
					arrow.y = rows * (spacingY + thumbHeight) - spacingY + margin;
					arrow.rotation = 180;				
					break;
				case Thumbs.HORIZONTAL : 
					arrow = createArrow(DECR);
					arrow.x = -margin;
					arrow.y = rows * (spacingY + thumbHeight) * 0.5;
					arrow.rotation = -90;

					arrow = createArrow(INCR);
					arrow.x = columns * (spacingX + thumbWidth) - spacingX + margin;
					arrow.y = rows * (spacingY + thumbHeight) * 0.5;
					arrow.rotation = 90;
					break;
			}
		}

		override public function clear() : void {
			if (_maxScroll <= 0) {
				return;
			}
			var a : Arrow;
			a = _thumbs.removeChild(_thumbs.getChildByName(INCR)) as Arrow;
			removeArrowEvents(a);
			a = _thumbs.removeChild(_thumbs.getChildByName(DECR)) as Arrow;
			removeArrowEvents(a);
		}

		private function createArrow(name : String) : Arrow {
			var a : Arrow = new Arrow();
			a.name = name;
			a.buttonMode = true;
			registerArrowEvents(a);
			_thumbs.addChild(a);
			return a;
		}

		private function hexToRGB (hex : uint) : Object {
			var red : int = hex>>16;
	        var grnBlu : int = hex-(red<<16);
	        var grn : int = grnBlu>>8;
	        var blu : int = grnBlu-(grn<<8);
	        return {r:red, g:grn, b:blu};
		}
		private function registerArrowEvents(dispatcher : IEventDispatcher) : void {
			dispatcher.addEventListener(MouseEvent.CLICK, onClickArrow);
			//dispatcher.addEventListener(MouseEvent.MOUSE_OVER, onOverArrow, false, 0, true);
			//dispatcher.addEventListener(MouseEvent.MOUSE_OUT, outOutArrow, false, 0, true);
		}
		private function removeArrowEvents(dispatcher : IEventDispatcher) : void {
			dispatcher.removeEventListener(MouseEvent.CLICK, onClickArrow);
			//dispatcher.removeEventListener(MouseEvent.MOUSE_OVER, onOverArrow, false, 0, true);
			//dispatcher.removeEventListener(MouseEvent.MOUSE_OUT, outOutArrow, false, 0, true);
		}
		
		/**
		 * Events
		 */
		private function onClickArrow(e : MouseEvent) : void {
			switch (e.currentTarget.name){
				case DECR:
					if(--_thumbs.curScroll < 0) { 
						_thumbs.curScroll = 0 ;
					}
					break;
				case INCR:
					if(++_thumbs.curScroll > _maxScroll) { 
						_thumbs.curScroll = _maxScroll ;
					}
					break;
			}
			_thumbs.moveHolder();
		}
	}
}
